package com.lanchewuyou.user.driver.lipeiui;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.afollestad.materialdialogs.MaterialDialog;
import com.lanchewuyou.user.driver.R;
import com.lanchewuyou.user.driver.modul.Config;
import com.lanchewuyou.user.driver.modul.OkHttpClientManager;
import com.lanchewuyou.user.driver.ui.CarPhotoActivity;
import com.squareup.okhttp.Request;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

/**
 * 还车订单详情页
 */

public class ReturnCarActivity extends AppCompatActivity implements View.OnClickListener {
    private Toolbar mToolbar;
    private TextView returnDriverName, returnDriverPhone, returnOwnerName, returnIsPay,
            returnOwnerPhone, returnCaseType, returnPickAdd, returnTime, returnAdd, returnMarked;
    private Button returnIknow, returnNextBtn;
    private LinearLayout returnCarCall, returnCarMap;
    private Toast toast1 = null;
    public static String ORDERID;
    private static String DRIVERID;
    private ProgressDialog progressDialog, progressUploadDialog;

    private void showTextToast(String msg) {
        if (toast1 == null) {
            toast1 = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
        } else {
            toast1.setText(msg);
        }
        toast1.show();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_return_car);
        initViews();
        progressDialog.show();
        Intent intent = getIntent();
        String caseId = intent.getStringExtra("caseid");
        ORDERID = caseId;
        SharedPreferences sharedPreferences = getSharedPreferences(Config.PASSWORD_PATH, Activity.MODE_PRIVATE);
        DRIVERID = sharedPreferences.getString("driverid", "");
        OkHttpClientManager.getAsyn(Config.DRIVERORDERDETAIL + "&driverid=" + DRIVERID + "&orderid=" + caseId, new OkHttpClientManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                showTextToast("载入失败");
                progressDialog.dismiss();
            }

            @Override
            public void onResponse(String response) {
                Log.d("Pickcar", response);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String status = jsonObject.getString("status");
                    if (status.equals("1")) {
                        JSONObject info = jsonObject.getJSONObject("info");
                        returnDriverPhone.setText(info.getString("ba_mobile"));
                        returnDriverName.setText(info.getString("username"));
                        returnOwnerName.setText(info.getString("chezhu_name"));
                        returnOwnerPhone.setText(info.getString("mobile"));
                        String accident = info.getString("accident");
                        if (accident.equals("1")) {
                            returnCaseType.setText("双车事故（第三者）");
                        } else if (accident.equals("2")) {
                            returnCaseType.setText("单车事故");
                        } else if (accident.equals("3")) {
                            returnCaseType.setText("双车事故");
                        }
                        returnPickAdd.setText(info.getJSONObject("shopinfo").getString("address"));
                        returnTime.setText(info.getString("expect_huanchetime"));
                        returnMarked.setText(info.getString("remark"));
                        returnAdd.setText(info.getString("address"));
                        String isPeikuan = info.getString("ifindemnity");
                        if (isPeikuan.equals("0")) {
                            returnIsPay.setText("需要赔款");
                        } else if (isPeikuan.equals("1")) {
                            returnIsPay.setText("不需要赔款");
                        }
                        progressDialog.dismiss();
                    } else {
                        Log.d("returncar", "error");
                        progressDialog.dismiss();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    progressDialog.dismiss();
                }
            }
        });
    }

    private void initViews() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbar.setTitle("信息确认");
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        returnIknow = (Button) findViewById(R.id.return_i_know);
        returnIknow.setOnClickListener(this);
        returnNextBtn = (Button) findViewById(R.id.return_next_btn);
        returnNextBtn.setOnClickListener(this);
        returnIsPay = (TextView) findViewById(R.id.return_ispeikuan);
        returnDriverName = (TextView) findViewById(R.id.return_driver_name);
        returnDriverPhone = (TextView) findViewById(R.id.return_driver_phone);
        returnOwnerName = (TextView) findViewById(R.id.return_owner_name);
        returnOwnerPhone = (TextView) findViewById(R.id.return_owner_phone);
        returnCaseType = (TextView) findViewById(R.id.return_case_type);
        returnPickAdd = (TextView) findViewById(R.id.return_pick_add);
        returnTime = (TextView) findViewById(R.id.return_time);
        returnAdd = (TextView) findViewById(R.id.return_add);
        returnMarked = (TextView) findViewById(R.id.return_marked);
        progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("载入中");
        progressDialog.setMessage("请稍等");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setCancelable(true);
        progressUploadDialog = new ProgressDialog(this);
        progressUploadDialog.setTitle("上传中");
        progressUploadDialog.setMessage("请稍等");
        progressUploadDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressUploadDialog.setCancelable(true);
        returnCarCall = (LinearLayout) findViewById(R.id.return_call_custom);
        returnCarCall.setOnClickListener(this);
//        returnCarMap = (LinearLayout) findViewById(R.id.return_car_map);
//        returnCarMap.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.return_i_know:
                finish();
                break;
            case R.id.return_next_btn:
                progressUploadDialog.show();
                Log.d("return", Config.RETURNCARINFOSTATUS + ORDERID + "&driverid=" + DRIVERID);
                OkHttpClientManager.getAsyn(Config.RETURNCARINFOSTATUS + ORDERID + "&driverid=" + DRIVERID,
                        new OkHttpClientManager.StringCallback() {
                            @Override
                            public void onFailure(Request request, IOException e) {
                                showTextToast("服务器出了一点问题");
                                progressUploadDialog.dismiss();
                            }

                            @Override
                            public void onResponse(String response) {
                                Log.d("return", response);
                                try {
                                    JSONObject jsonObject = new JSONObject(response);
                                    String status = jsonObject.getString("status");
                                    if (status.equals("1")) {
                                        Intent intent1 = new Intent(ReturnCarActivity.this, CarPhotoActivity.class);
                                        intent1.putExtra("from", "returncar");
                                        startActivity(intent1);
                                        finish();
                                        progressUploadDialog.dismiss();
                                    } else {
                                        progressUploadDialog.dismiss();
                                        showTextToast("请求失败");
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                    progressUploadDialog.dismiss();
                                }

                            }
                        });
                break;
            case R.id.return_call_custom:
                Intent i = new Intent(Intent.ACTION_DIAL);
                i.setData(Uri.parse("tel:" + returnDriverPhone.getText().toString()));
                startActivity(i);
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_return_car, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        showBackDialog();
    }

    private void showBackDialog() {
        new MaterialDialog.Builder(this)
                .title("退出任务")
                .content("点击确定将退出您正在执行的任务。")
                .positiveText("确定")
                .negativeText("取消")
                .callback(new MaterialDialog.ButtonCallback() {
                    @Override
                    public void onPositive(MaterialDialog dialog) {
                        finish();
                    }

                    @Override
                    public void onNegative(MaterialDialog dialog) {
                        super.onNegative(dialog);
                    }
                })
                .show();
    }


}
